package com.songcha.module_mine.ui.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Shader
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.lifecycle.ViewModel
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.LayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.songcha.library_base.handler.ApiObservableHandler
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_business.util.MajiaBaoUtil
import com.songcha.library_common.ui.dialog.CustomDialog
import com.songcha.library_common.ui.view.CustomButton
import com.songcha.library_common.util.ScreenUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_network.bean.BaseBean
import com.songcha.module_mine.R
import com.songcha.module_mine.api.MineApiManager
import com.songcha.module_mine.bean.GoldExchangeVipBean
import com.songcha.module_mine.ui.adapter.GoldExchangeAdapter

class GoldExchangeDialog(context: Context,val mList:MutableList<GoldExchangeVipBean>):CustomDialog(context) ,OnItemChildClickListener{
    private var exchangeListener:((Int)->Unit)?=null
    private var mTvMyGold:TextView?=null

    init {
        setDialogTitle("")
        setDialogMsg("")
        setShowBtn(false)
        setWidth(332.dp2px())
        setHeight(480.dp2px())
        setRadius(0f)
        setBackgroundColor(Color.TRANSPARENT)
        setDimAmount(0f)
    }

    fun setExchangeListener(listener:((Int)->Unit)){
        exchangeListener=listener
    }

    override fun hasView(): Boolean {
        return true
    }

    override fun getView(root: ViewGroup): View? {
        val view=LayoutInflater.from(context).inflate(R.layout.mine_dialog_gold_exchange,root,false)


        val tvTitle=view.findViewById<TextView>(R.id.mine_tv_title)
        val majiaName = MajiaBaoUtil.getMajiaName()
        var titleStartColor = Color.parseColor("#B687F0")
        var titleEndColor = Color.parseColor("#9A7DFA")
        if(majiaName == "beike"){
            titleStartColor = Color.parseColor("#F67575")
            titleEndColor = Color.parseColor("#FA7DAC")
        }
        val shader =  LinearGradient(0f, 0f, 0f, tvTitle.textSize,titleStartColor , titleEndColor, Shader.TileMode.CLAMP)
        tvTitle.paint.shader = shader

        mTvMyGold=view.findViewById<TextView>(R.id.mine_tv_mygold).apply {
            if(UserInfoManager.checkLogin())
                text="我的金币："+UserInfoManager.userInfo!!.gold.toString()
        }

        view.findViewById<ImageView>(R.id.mine_iv_close).setOnClickListener {
            dismiss()
        }

        view.findViewById<CustomButton>(R.id.mine_btn).setOnClickListener {
            dismiss()
        }

        val rcv=view.findViewById<RecyclerView>(R.id.mine_rcv)
        rcv.apply{
            layoutManager= LinearLayoutManager(context)
            adapter=GoldExchangeAdapter(mList).apply {
                setOnItemChildClickListener(this@GoldExchangeDialog)
            }
        }

        return view
    }

    fun updateMyGold(){
        mTvMyGold?.let {
            if(UserInfoManager.checkLogin())
                it.text=UserInfoManager.userInfo!!.gold.toString()
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        when(view.id){
            R.id.mine_btn_exchange->{
                exchangeListener?.let {
                    it(position)
                }
            }
        }
    }


}